package com.xxbb.chapter_11.dome3_socket;

import com.xxbb.util.tool.Tool;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try (
                Socket socket = new Socket(InetAddress.getLocalHost(), 9000);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream());
                //创建键盘输入流
                BufferedReader wt = new BufferedReader(new InputStreamReader(System.in))) {
            while (true) {
                Tool.println("接收到消息！");
                String str = wt.readLine();//从键盘读取一行
                out.println(str);//把字符串发送给服务器
                out.flush();//刷空输出流
                if (str.equals("end")) {
                    break;
                }
                System.out.println("对方发来："+in.readLine());

            }

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
